using System.Linq;
using MRGSP.SurveyMaster.Core.Service;
using Omu.ValueInjecter;
using System.Web.Mvc;

namespace MRGSP.SurveyMaster.Infra.ValueInjections
{
    public class RootTypesInjection : NoSourceValueInjection
    {
        readonly string prefix;

        public RootTypesInjection(string prefix)
        {
            this.prefix = prefix;
        }

        protected override void Inject(object target)
        {
            var targetProps = target.GetProps();
            for (var i = 0; i < targetProps.Count; i++)
            {
                var currentTarget = targetProps.GetByName(prefix + "Type");

                if (currentTarget == null || currentTarget.PropertyType != typeof(object)) continue;

                var value = IoC.Resolve<IControlTypeService>().GetRootTypes().Select(o =>
                                                                                     new SelectListItem
                                                                                         {
                                                                                             Text = o.Name,
                                                                                             Value = o.Id.ToString()
                                                                                         });
                currentTarget.SetValue(target, value);

            }
        }
    }
}